package com.zerui.manager.deviceTouchSync.gatewayimpl;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zerui.manager.deviceTouchSync.DeviceTouchSync;
import com.zerui.manager.deviceTouchSync.convertor.DeviceTouchSyncConvert;
import com.zerui.manager.deviceTouchSync.entity.DeviceTouchSyncDO;
import com.zerui.manager.deviceTouchSync.gateway.DeviceTouchSyncGateWay;
import com.zerui.manager.deviceTouchSync.mapper.DeviceTouchSyncMapper;
import com.zerui.manager.web.enums.DeviceSyncStatusEnum;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

@Component
public class DeviceTouchSyncGateWayImpl implements DeviceTouchSyncGateWay {

    @Resource
    private DeviceTouchSyncMapper deviceTouchSyncMapper;

    @Override
    public Long addDeviceTouchSync(DeviceTouchSync deviceTouchSync) {
        DeviceTouchSyncDO deviceTouchSyncDO = DeviceTouchSyncConvert.INSTANCE.convertDeviceTouchToDeviceTouchDo(deviceTouchSync);
        deviceTouchSyncMapper.insert(deviceTouchSyncDO);
        return deviceTouchSyncDO.getId(); // 返回主键
    }

    @Override
    public DeviceTouchSync selectById(Long id) {
        DeviceTouchSyncDO deviceTouchSyncDO = deviceTouchSyncMapper.selectById(id);
        return DeviceTouchSyncConvert.INSTANCE.convertDeviceTouchDoToDeviceTouch(deviceTouchSyncDO);
    }

    @Override
    public void deleteById(Long id) {
        deviceTouchSyncMapper.deleteById(id);
    }

    @Override
    public void syncStatus(Long id, Integer syncStatusCode, LocalDateTime now) {
        LambdaUpdateWrapper<DeviceTouchSyncDO> updateWrapper = Wrappers.lambdaUpdate(DeviceTouchSyncDO.class);
        updateWrapper.set(DeviceTouchSyncDO::getSyncStatus, syncStatusCode)
                .set(syncStatusCode == DeviceSyncStatusEnum.SYNC_DOING.getCode(), DeviceTouchSyncDO::getSyncTime, now)
                .eq(DeviceTouchSyncDO::getId, id);
        deviceTouchSyncMapper.update(null, updateWrapper);
    }

    @Override
    public void updateSyncInfo(DeviceTouchSync touchSync) {
        LambdaUpdateWrapper<DeviceTouchSyncDO> updateWrapper = Wrappers.lambdaUpdate(DeviceTouchSyncDO.class);
        updateWrapper.set(DeviceTouchSyncDO::getSyncStatus, touchSync.getSyncStatus())
                .set(DeviceTouchSyncDO::getSyncFromData,touchSync.getSyncFromData())
                .set(DeviceTouchSyncDO::getSyncAddData, touchSync.getSyncAddData())
                .set(DeviceTouchSyncDO::getSyncUpdateData, touchSync.getSyncUpdateData())
                .set(DeviceTouchSyncDO::getSyncDeleteData, touchSync.getSyncDeleteData())
                .set(DeviceTouchSyncDO::getSyncRemark, touchSync.getSyncRemark())
                .eq(DeviceTouchSyncDO::getId, touchSync.getId());
        deviceTouchSyncMapper.update(null, updateWrapper);
    }


}
